import { lazyReportBatch } from "../report";

export default function observerFCP() {
  const entryHandler = (list) => {
    for (const entry of list.getEntries()) {
      if (entry.name === "first-contentful-paint") {
        observer.disconnect();
        const json = entry.toJSON();
        console.log("First Content Paint:", json);
        //   上报
        const reportData = {
          ...json,
          type: "performance",
          subType: entry.name,
          pageUrl: window.location.href,
        };
        lazyReportBatch(reportData);
      }
    }
  };

  // 统计和计算FCP( First Content Paint) 的性能指标
  const observer = new PerformanceObserver(entryHandler);

  // buffered:true 确保在页面加载时也能捕获到之前的绘制事件
  observer.observe({
    type: "paint",
    buffered: true,
  });
}
